﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CommonUnity
{
    /// <summary>
    /// 验证帮助类
    /// </summary>
    public class ValidateHelper
    {
        #region 验证身份证号码
        /// <summary>
        /// 验证身份证号码
        /// </summary>
        /// <param name="Id">身份证号码</param>
        /// <returns>验证成功为True，否则为False</returns>
        public static bool CheckIDCard(string Id)
        {
            long n = 0;
            if (long.TryParse(Id.Remove(17), out n) == false || n < Math.Pow(10, 16) || long.TryParse(Id.Replace('x', '0').Replace('X', '0'), out n) == false)
            {
                return false;//数字验证
            }
            string address = "11x22x35x44x53x12x23x36x45x54x13x31x37x46x61x14x32x41x50x62x15x33x42x51x63x21x34x43x52x64x65x71x81x82x91";
            if (address.IndexOf(Id.Remove(2)) == -1)
            {
                return false;//省份验证
            }
            string birth = Id.Substring(6, 8).Insert(6, "-").Insert(4, "-");
            DateTime time = new DateTime();
            if (DateTime.TryParse(birth, out time) == false)
            {
                return false;//生日验证
            }
            string[] arrVarifyCode = ("1,0,x,9,8,7,6,5,4,3,2").Split(',');
            string[] Wi = ("7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2").Split(',');
            char[] Ai = Id.Remove(17).ToCharArray();
            int sum = 0;
            for (int i = 0; i < 17; i++)
            {
                sum += int.Parse(Wi[i]) * int.Parse(Ai[i].ToString());
            }
            int y = -1;
            Math.DivRem(sum, 11, out y);
            if (arrVarifyCode[y] != Id.Substring(17, 1).ToLower())
            {
                return false;//校验码验证
            }
            return true;//符合GB11643-1999标准
        }
        #endregion

        #region 验证乘客姓名是否为繁体字
        /// <summary>
        /// 验证乘客姓名是否为繁体字
        /// </summary>
        /// <param name="name">乘客姓名</param>
        /// <returns>非GB2312的字符</returns>
        public static string GB2312_Check(string name)
        {
            string temp1 = Regex.Replace(name, "[A-Za-z]|/| ", "");
            string[] specialStr = { "貟", "璠", "啟" };
            foreach (var spe in specialStr)
            {
                if (temp1.Contains(spe))
                {
                    temp1 = temp1.Replace(spe, "");
                }
            }
            string temp = temp1;
            if (temp.Length == 0)
                return string.Empty;
            string strError = string.Empty;
            byte[] bttemp = Encoding.Default.GetBytes(temp);
            for (int i = 0; i < bttemp.Length; i = i + 2)
            {
                if (bttemp[i] >= 176 && bttemp[i] <= 247 &&
                    bttemp[i + 1] >= 160 && bttemp[i + 1] <= 254)
                {
                    continue;
                }
                else
                {
                    strError += temp[i / 2];
                }
            }
            return strError;
        }
        #endregion

        #region 验证乘客姓名是否合法
        /// <summary>
        /// 验证乘客姓名是否合法
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool CheckPassNameLegal(string name)
        {
            Regex rg = new Regex("^[A-Za-z-\u4e00-\u9fa5/]+$");
            if (rg.IsMatch(name))
                return true;
            else
                return false;
        }
        #endregion

        #region 证件号码验证
        /// <summary>
        /// 验证证件号码
        /// </summary>
        /// <param name="certNoStr">证件号码</param>
        /// <param name="reCertNoStr">处理后的证件号码字符串</param>
        /// <returns></returns>
        public static bool CheckCertNo(string certNoStr, out string reCertNoStr)
        {
            reCertNoStr = string.Empty;
            if (certNoStr == null)
                return false;
            bool isTrue = false;
            reCertNoStr = ToDBC(certNoStr);//将全角转成半角
            reCertNoStr = Regex.Replace(reCertNoStr, "([ ]+)", "");//过滤半角空格
            reCertNoStr = Regex.Replace(reCertNoStr, "([　]+)", "");//过滤全角空格
            isTrue = Regex.IsMatch(reCertNoStr, @"^[\(\)\/(A-Z)a-z0-9]+$");
            return isTrue;
        }
        #endregion

        #region 全角转半角
        /// <summary>
        /// 全角转半角
        /// </summary>
        /// <param name="input">需要处理字符串</param>
        /// <returns></returns>
        /// 全角空格为12288，半角空格为32
        /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        public static String ToDBC(String input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new String(c);
        }
        #endregion

        #region 验证qdid
        /// <summary>
        /// 验证qdid
        /// </summary>
        /// <param name="qdid">qdid</param>
        /// <returns></returns>
        public static string VertifyQdid(string qdid)
        {
            //可以进行配置，方便日后进行新增别的数字
            List<string> numqdid = new List<string>() { "-8888", "-9999", "-88888", "-99999" };
            if (string.IsNullOrWhiteSpace(qdid))
            {
                return string.Empty;
                //-待定默认值
                //return "-9999";
            }
            string result = string.Empty;
            qdid = qdid.Replace("，", ",");
            string[] qdids = qdid.Split(',');
            if (qdids.Length > 3)
            {
                //取最后3个
                qdids = qdids.Skip(qdids.Length - 3).Take(3).ToArray();
            }

            foreach (string id in qdids)
            {
                //无效判断
                if (id.Split('|').Length != 4/*或者进行正则判断*/ && !numqdid.Contains(id))
                {
                    result += "-8888,";
                }
                else
                {
                    result += string.Format("{0},", id.Trim());
                }
            }
            result = result.TrimEnd(',');
            return result;
        }
        #endregion
    }
}
